//操作界面
var DN_CMD = require("zjhCMD");
cc.Class({
    extends: cc.Component,

    properties: {
        className:"optlayer",

        jiaofen_altas:{
            default:null,
            type:cc.SpriteAtlas
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        console.log("重新创建");
        this.node.thisJS = this;
        this.readylayout =  cc.find("readylayout",this.node);
        this.jiaofenlayout = cc.find("jiaofenlayout",this.node)
        this.bipailayout = cc.find("bipailayout",this.node);
        this.qipai = cc.find("qipai",this.bipailayout);
        this.bipai = cc.find("bipai",this.bipailayout);
        this.bipailayout.active = true ;
        this.qipai.active = false ;
        this.bipai.active = false ;
        this.initUI();
    },
    start: function () {
    },
    initUI: function(){
        console.log("接收监听 开始游戏");
        EventManager.addEventObeserveF(EventConfig.UserEnterDeskNtf, handler(this,
            this.userJoin),"optlayer");
        EventManager.addEventObeserveF(EventConfig.startGame, handler(this,
            this.gameStart),"optlayer");
        EventManager.addEventObeserveF(EventConfig.xiaZhu, handler(this,
            this.xiaZhuNtf),"optlayer");
        EventManager.addEventObeserveF(EventConfig.clearTable, handler(this,
            this.clearTable),"optlayer");
        EventManager.addEventObeserveF(EventConfig.qiPai, handler(this,
            this.qiPaiNtf),"optlayer");
        EventManager.addEventObeserveF(EventConfig.compareNtf, handler(this,
            this.compareNtf),"optlayer");
        EventManager.addEventObeserveF(EventConfig.roundOverCompareCard, handler(this,
            this.compareNtf),"optlayer");
        EventManager.addEventObeserveF(EventConfig.kanPaiNtf, handler(this,
            this.kanPaiNtf),"optlayer");

    },
    //玩家加入 重连
    userJoin:function(data){
         var info = GameData ;
        //判断是否应该显示准备按钮
        for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==UserInfoModel.info.uin){
                var user = info.playerinfo[k];
                if(user.status == GameConfig.playerStatus.USER_STATE_READY){
                    this.readylayout.active = true ;
                    console.log("显示准备按钮");
                }
                else{
                    console.log("关闭准备按钮");
                    this.readylayout.active = false ;
                }
            }
         }

        if(data.next_uin && data.next_uin>0)
        {
             var user =  GameData.getUserByUin(data.next_uin);
             var searId = GameData.getUserSeatP(user.seatid);
             if(data.player_op_past_time && data.player_op_past_time>0)
             {  
                this.startJiaoFen(data);     
                this.startOP(data);
             }
        }
        //当前操作是我的话要显示操作框
    },
    kanPaiNtf:function(data){
        if(data.uin == UserInfoModel.info.uin)
        {
            this.isKanPai = true ;
            if(this.call_money){
                this.call_money = this.call_money * 2 ;
            }
            this.startJiaoFen({});
        }
    },
    //玩家进入桌子,更新个人信息  
    gameStart:function(data){
        Log.logd("opt gameStart");
        this.readylayout.active = false ;
        console.log("data.can_operator1->" +(data.can_operator & 0x2));
        console.log("data.can_operator2->" +(data.can_operator & 0x1));
        this.scheduleOnce(
            function(){
                //设置下注
                if(data.next_uin){
                    if(UserInfoModel.info.uin==data.next_uin){
                        this.startJiaoFen(data);
                        this.startOP(data);
                    }
                }
        },2)
       
    },
    //请求准备
    readyReq:function(){
        console.log("发送准备请求");
        GameNet.sendRequest({cmd:DN_CMD.UserReadyReq,
            body:{desk_id:GameData.deskid,
                callback:function(info){
            Log.logd("readyReq收到回调");
        }}});
        this.readylayout.active = false ;
    },
    /*
    // cmd: 1710 用户请求弃牌
    message GoldFlowerUserFoldReq{
        optional int32 uin = 1;             //用户ID
        optional int32 desk_id=2;           //桌子ID
    }
    }*/
    //比牌
    biPaiReq:function(){
        console.log("biPaiClick");
        EventManager.dispatchEventF(EventConfig.biPaiClick,"");
    },
    //弃牌
    //  optional int32 uin = 1;             //用户ID
    //  optional int32 desk_id=2;           //桌子ID
    qiPaiReq:function(){
        GameNet.sendRequest({cmd:DN_CMD.UserFoldReq,
            body:{
                uin: UserInfoModel.info.uin,
                desk_id:GameData.deskid,
                }});
    },
    qiPaiNtf:function(data){
      if(data.uin == UserInfoModel.info.uin){
        this.jiaofenlayout.active = false ;
        this.qipai.active = false ;
        this.bipai.active = false ;
      }
      if(data.next_uin == UserInfoModel.info.uin){
        this.startJiaoFen(data);
        this.startOP(data);
      }
    },
    compareNtf:function(data){
        this.jiaofenlayout.active = false ;
        this.qipai.active = false ;
        this.bipai.active = false ;
    },
    startJiaoFen:function(data){
       // var ban_name = "font15_o";
      //  var bgname = ["font22_v","font14_n","font13_m","font23_w"];
        this.jiaofenlayout.active = true ;
        console.log("开始叫分");
        if(data.call_money){
            this.call_money = data.call_money ;
            if(this.isKanPai){
                this.call_money = this.call_money*2 ;
            }
        }
        console.log("this->call_money"+this.call_money);
        for(i=0;i<GameData.gold_score_list.length;i++)
        {   
            var score = GameData.gold_score_list[i] ;
            if(this.isKanPai){
                score = score*2 ;
            }
            var btn = cc.find("btn"+(i+1),this.jiaofenlayout);
            var label = cc.find("Label",btn);
            if(this.isKanPai){
                label.getComponent(cc.Label).string = GameData.gold_score_list[i]*2+"分";
            }else{
                label.getComponent(cc.Label).string = GameData.gold_score_list[i]+"分";
            }
            btn.active = true ;
            if(this.call_money==null){
              //  btn.getComponent(cc.Sprite).spriteFrame = this.jiaofen_altas.getSpriteFrame(bgname[i]);
               btn.getComponent(cc.Button).interactable = true; 
               btn.getComponent(cc.Button).enabled = true;  
               cc.find("mask",btn).active = false;
            }else{
                if(score>=this.call_money){
                    btn.getComponent(cc.Button).interactable = true; 
                    btn.getComponent(cc.Button).enabled = true;  
                    cc.find("mask",btn).active = false;
                }else{
                    btn.getComponent(cc.Button).interactable = false; 
                    btn.getComponent(cc.Button).enabled = false;  
                    cc.find("mask",btn).active = true;
                }
            }
        }

        this.gold_score_list = GameData.gold_score_list ;
    },
    /*
    optional int32 uin = 1;         //用户ID
    optional int32 desk_id=2;       //桌子ID
    optional int32 call_money=3;    //叫分倍数
    */
    jiaofenClick:function(event,index){
        var call_money = this.gold_score_list[index] ;
        if(this.isKanPai){
            call_money = call_money*2 ;
        } 
        if(call_money<this.call_money){
            return ;
        }
        console.log("下注-->"+call_money);
        GameNet.sendRequest({cmd:DN_CMD.GoldFlowerUserChipReq,
            body:{
                uin: UserInfoModel.info.uin,
                desk_id:GameData.deskid,
                call_money:call_money,
           }});
        this.jiaofenlayout.active = false ;
    },
    /*
    message GoldFlowerUserChipNtf{
    optional int32 uin = 1;             //用户ID
    optional int32 desk_id=2;           //桌子ID
    optional int64 call_money=3;        //用户下注的钱
    optional int32 round_count=4;       //当前轮数
    optional int32 max_count=5;         //最大轮数
    optional int64 user_money=6;        //用户当前下的钱
    optional int64 desk_total_chip=7;   //桌子当前总注数
    optional int32 next_uin=8;          //下一个操作的玩家  如果为-1 表示当局已经结束 等待game_over指令
    optional int32 can_operator = 9;        // 可以执行的操作 包括 看牌 比牌 弃牌  用位数表示  11  第一位看牌  第二位比牌
    }   
    */
    xiaZhuNtf:function(data){
        this.jiaofenlayout.active = false ;
        this.qipai.active = false ;
        this.bipai.active = false ;
        if(data.next_uin){
            if(UserInfoModel.info.uin==data.next_uin){
                this.startJiaoFen(data);
                this.startOP(data);
            }
        }
    },
   // console.log("data.can_operator1->" +(data.can_operator & 0x2));
   // console.log("data.can_operator2->" +(data.can_operator & 0x1));
    startOP:function(data){
     var isKP = Math.floor(data.can_operator /10);
     var isBP =  data.can_operator %10 ;
     console.log("弃牌-->"+isKP);
     console.log("弃牌-->"+isBP);
     this.qipai.active = true ;
     this.bipai.active = true ;
     if(isBP==1){
        this.bipai.getComponent(cc.Button).interactable = true; 
        this.bipai.getComponent(cc.Button).enabled = true; 
        cc.find("mask",this.bipai).active = false;
     }else{
        this.bipai.getComponent(cc.Button).interactable = false; 
        this.bipai.getComponent(cc.Button).enabled = false; 
        cc.find("mask",this.bipai).active = true;
     }
    },
    //清桌
    clearTable:function(){
        console.log("optlayer clearTable");
        this.readylayout.active = true;
        this.jiaofenlayout.active = false ;
        this.bipailayout.active = true ;
        this.qipai.active = false ;
        this.bipai.active = false ;
        this.isKanPai = false ;
        this.call_money = null ;
    },
});
